class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();
        if (n == 1)
            return nums[0] == target ? 0 : -1;
        int left = 0, right = n - 1;

        while(left <= right)
        {
            int mid = (left + right) >> 1;
            if(target == nums[mid])
                return mid;

            if(nums[0] <= nums[mid]) // 左边是有序数组
            {
                if(target >= nums[0] && target < nums[mid])
                    right = mid - 1;
                else
                    left = mid + 1;
            }
            else // 右边是有序数组
            {
                if(target > nums[mid] && target <= nums[n - 1])
                    left = mid + 1;
                else
                    right = mid - 1;
            }
        }
        return -1;
    }
};